﻿using Abp.Auditing;
using Abp.Events.Bus.Entities;
using Abp.Zero.EntityFrameworkCore;
using AbpProjectTemplate.Components.Auditing;
using AbpProjectTemplate.Domain.Categories;
using AbpProjectTemplate.Domain.Roles;
using AbpProjectTemplate.Domain.Users;
using AbpProjectTemplate.Domain.Users.Admins;
using AbpProjectTemplate.Domain.Users.Members;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
using AbpProjectTemplate.Components.Events.Entities;
using AbpProjectTemplate.Components.MultiTenancy;

namespace AbpProjectTemplate.EntityFrameworkCore
{
    public class AppDbContext : AbpZeroDbContext<Tenant, Role, User, AppDbContext>
    {
        /* Define a DbSet for each entity of the application */

        public AppDbContext(DbContextOptions<AppDbContext> options)
            : base(options)
        {
        }
        public DbSet<Admin> Admins { get; set; }
        public DbSet<Member> Members { get; set; }
        public DbSet<Category> Categories { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //过滤初始化dbcontext及导航属性警告
            optionsBuilder.ConfigureWarnings(t =>
            {
                t.Ignore(CoreEventId.PossibleIncorrectRequiredNavigationWithQueryFilterInteractionWarning,
                         CoreEventId.ContextInitialized);
            });
#if DEBUG
            optionsBuilder.EnableSensitiveDataLogging();
#endif
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasDiscriminator(t => t.UserType)
                .HasValue<Admin>(UserType.Admin)
                .HasValue<Member>(UserType.Member);

            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<AuditLog>()
             .Property(e => e.Parameters)
             .HasMaxLength(AppAuditingStore.MaxParametersLength);
        }

        protected override void ApplyAbpConceptsForAddedEntity(EntityEntry entry, long? userId, EntityChangeReport changeReport)
        {
            base.ApplyAbpConceptsForAddedEntity(entry, userId, changeReport);
            var appChangeReport = new AppEntityChangeEntry(entry.Entity, EntityChangeType.Created);
            this.EntityChangeEventHelper.TriggerEntityChangeCheckEvent(appChangeReport);
        }

        protected override void ApplyAbpConceptsForModifiedEntity(EntityEntry entry, long? userId, EntityChangeReport changeReport)
        {
            base.ApplyAbpConceptsForModifiedEntity(entry, userId, changeReport);
            var appChangeReport = new AppEntityChangeEntry(entry.Entity, EntityChangeType.Updated, entry.OriginalValues.ToObject());
            this.EntityChangeEventHelper.TriggerEntityChangeCheckEvent(appChangeReport);
        }

        protected override void ApplyAbpConceptsForDeletedEntity(EntityEntry entry, long? userId, EntityChangeReport changeReport)
        {
            base.ApplyAbpConceptsForDeletedEntity(entry, userId, changeReport);
            var appChangeReport = new AppEntityChangeEntry(entry.Entity, EntityChangeType.Deleted);
            this.EntityChangeEventHelper.TriggerEntityChangeCheckEvent(appChangeReport);
        }
    }
}

